package studydemo.config.listener;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import studydemo.service.listener.MyAckReceiverMethod2Service;

import javax.annotation.Resource;

/**
 * 监听多个队列的配置类
 *
 * @author xn
 * @date 2022/11/21 20:20
 */
@Configuration
public class MessageListenerTestConfig {

    @Resource
    private CachingConnectionFactory connectionFactory;
    //消息接收处理类
    @Resource
    private MyAckReceiverMethod2Service myAckReceiverMethod2Service;

    @Bean
    public SimpleMessageListenerContainer testSimpleMessageListenerContainer() {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        container.setConcurrentConsumers(1);
        container.setMaxConcurrentConsumers(1);
        // RabbitMQ默认是自动确认，这里改为手动确认消息
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        //设置(监听哪)一个队列
        container.setQueueNames("TestDirectQueue","fanout.A");
        //如果同时设置多个如下： 前提是队列都是必须已经创建存在的
        //方法1
        //  container.setQueueNames("lonelyDirectQueue","lonelyDirectQueue2","lonelyDirectQueue3");

        //方法2
        // 另一种设置队列的方法,如果使用这种情况,那么要设置多个,就使用addQueues
        //container.setQueues(new Queue("lonelyDirectQueue",true));
        //container.addQueues(new Queue("lonelyDirectQueue2",true));
        //container.addQueues(new Queue("lonelyDirectQueue3",true));

        container.setMessageListener(myAckReceiverMethod2Service);

        return container;
    }


}
